package com.yDev.stock_runner;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;



public class SR_Level {
	
	int i=0;
	
	ArrayList<RigidBody> rBodies =new ArrayList<RigidBody>();
	
	DataThread getDataThread;
	Paint paint=new Paint();
	
	int screenSize=480;
	int mapToCenter=0;
	
	ArrayList<RigidBody> terrian =new ArrayList<RigidBody>();
	
	RigidBody test=new RigidBody();
	
	public SR_Level(Context context,String sym){
		
		//new RequestTask().execute("http://finance.google.com/finance/info?client=ig&q=bvsn");
		getDataThread = new DataThread(context, "http://finance.google.com/finance/info?client=ig&q=FB", terrian, 0);
		test.position=new ydPoint(200, 400);
		test.size.x=200;
		test.size.y=20;
		
		test.theta=Globals.degreesToRads(40);
		test.MASS=2;
		
		test.calcCornerPoints();

		test.calculateCenterMass();
		
		Random rand=new Random(1000);
		
		for (int i=0; i<5; i++){
			RigidBody t=new RigidBody();
			//if(i==0){
				t.velocity=new ydPoint(0, 0.10f);
			//}
			t.position=new ydPoint(rand.nextInt(400), rand.nextInt(700));
			t.size.x=rand.nextInt(1)+5;
			t.size.y=rand.nextInt(1)+5;
			
			t.theta=Globals.degreesToRads(rand.nextInt(360));
			t.MASS=rand.nextInt(200)+50;
			
			t.calcCornerPoints();
			t.calculateCenterMass();
			
			rBodies.add(t);
		}
	}
	
	public void startDataCollection(){
		/*new RequestTask()
		.execute("http://finance.google.com/finance/info?client=ig&q=bvsn");*/
		getDataThread.start();
	}
	RigidBody r =new RigidBody();
	public void drawTerrian(Canvas canvas){
/*		r.position=new Point(100, 300);
		r.size=new Point(200, 50);
		r.calcCornerPoints();
		r.theta=Globals.degreesToRads(i);
		r.drawBody(canvas, paint);
		i++;*/
		if(terrian.size()==1){
			RigidBody r1=new RigidBody(terrian.get(0));
			mapToCenter=(int) (screenSize-r1.position.y);
		}
		if(terrian.size()>=1){
		RigidBody r1, prevR;
			for(int i=0; i<terrian.size(); i++){
				r1=new RigidBody(terrian.get(i));
				//r1.position.y+=mapToCenter;
				//r1.calcCornerPoints();
				if(i>0){
					r1.position=terrian.get(i-1).p3;
					r1.p2=terrian.get(i-1).p4;
				}
					
				r1.drawBody(canvas, paint);

			}
		}
		for(RigidBody r: rBodies){
			r.drawBody(canvas, paint);
		}
	//	test.calcCornerPoints();
		
		
		//test.drawBody(canvas, paint);
	}

	public void update() {
		
		Object[] ter=terrian.toArray();
		
		for(RigidBody r: rBodies){
			for(RigidBody r2: rBodies){
				RigidBody.collisionOccured(r, r2);
				r.calculateNextPosition(new ydPoint());
				r2.calculateNextPosition(new ydPoint());
			}
			for(int i=0; i<ter.length; i++){
				RigidBody.collisionOccured(r, (RigidBody)ter[i]);
				r.calculateNextPosition(new ydPoint());
			}
			/*for(RigidBody r2 :terrian){
				RigidBody.collisionOccured(r, r2);
				r.calculateNextPosition(new ydPoint());
				//r2.calculateNextPosition(new ydPoint());
			}*/
		}
		
		// TODO Auto-generated method stub
		//test.calculateAngularVelocityTotal();
		//test.calculateNextPosition(new ydPoint());
		
	}

}
